var _excluded = ["component", "playScale", "location", "targetId", "onScroll", "onChange", "replay", "componentProps"];
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? Object(arguments[r]) : {}, o = Object.keys(t); "function" == typeof Object.getOwnPropertySymbols && o.push.apply(o, Object.getOwnPropertySymbols(t).filter(function (e) { return Object.getOwnPropertyDescriptor(t, e).enumerable; })), o.forEach(function (r) { _defineProperty(e, r, t[r]); }); } return e; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == typeof e || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import EventListener from './EventDispatcher';
import { noop, currentScrollTop, transformArguments, windowHeight, windowIsUndefined } from './util';
var ScrollElement = /*#__PURE__*/function (_React$Component) {
  function ScrollElement(props) {
    var _this;
    _classCallCheck(this, ScrollElement);
    _this = _callSuper(this, ScrollElement, [props]);
    _defineProperty(_this, "getParam", function (e) {
      // 检查组件是否已卸载或 DOM 元素是否存在，如果不存在则直接返回
      if (_this.isUnmounted || !_this.dom) {
        return;
      }
      _this.clientHeight = _this.target ? _this.target.clientHeight : windowHeight();
      var scrollTop = _this.target ? _this.target.scrollTop : currentScrollTop();
      var domRect = _this.dom.getBoundingClientRect();
      var targetTop = _this.target ? _this.target.getBoundingClientRect().top : 0;
      var offsetTop = domRect.top + scrollTop - targetTop;
      _this.elementShowHeight = scrollTop - offsetTop + _this.clientHeight;
      var playScale = transformArguments(_this.props.playScale);
      var playScaleEnterArray = /([\+\-]?[0-9#\.]+)(px|vh|%)?/.exec(String(playScale[0])); // eslint-disable-line
      if (!playScaleEnterArray[2]) {
        _this.playHeight = _this.clientHeight * parseFloat(playScale[0]);
      } else if (playScaleEnterArray[2] === 'px') {
        _this.playHeight = parseFloat(playScaleEnterArray[1]);
      } else {
        _this.playHeight = _this.clientHeight * parseFloat(playScaleEnterArray[1]) / 100;
      }
      var leaveHeight = domRect.height;
      var playScaleLeaveArray = /([\+\-]?[0-9#\.]+)(px|vh|%)?/.exec(String(playScale[1])); // eslint-disable-line
      if (!playScaleLeaveArray[2]) {
        _this.leavePlayHeight = leaveHeight * parseFloat(playScale[1]);
      } else if (playScaleLeaveArray[2] === 'px') {
        _this.leavePlayHeight = parseFloat(playScaleLeaveArray[1]);
      } else {
        _this.leavePlayHeight = leaveHeight * parseFloat(playScaleLeaveArray[1]) / 100;
      }
      var enter = _this.props.replay ? _this.elementShowHeight >= _this.playHeight && _this.elementShowHeight <= _this.clientHeight + _this.leavePlayHeight : _this.elementShowHeight >= _this.playHeight;
      var enterOrLeave = enter ? 'enter' : 'leave';
      var mode = _this.enter !== enter || typeof _this.enter !== 'boolean' ? enterOrLeave : null;
      if (mode) {
        _this.props.onChange({
          mode: mode,
          id: _this.props.id
        });
      }
      _this.props.onScroll({
        domEvent: e,
        scrollTop: scrollTop,
        showHeight: _this.elementShowHeight,
        offsetTop: offsetTop,
        id: _this.props.id
      });
      _this.enter = enter;
    });
    _defineProperty(_this, "addScrollEvent", function () {
      EventListener.addEventListener(_this.eventType, _this.scrollEventListener, _this.target);
      var scrollTop = currentScrollTop();
      if (!scrollTop) {
        _this.scrollEventListener();
      }
    });
    _defineProperty(_this, "scrollEventListener", function (e) {
      _this.getParam(e);
    });
    _this.state = {
      $self: _this
    };
    _this.isUnmounted = false;
    return _this;
  }
  _inherits(ScrollElement, _React$Component);
  return _createClass(ScrollElement, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      if (windowIsUndefined) {
        return;
      }
      this.dom = ReactDOM.findDOMNode(this);
      var date = Date.now();
      this.target = this.props.targetId && document.getElementById(this.props.targetId);
      var length = EventListener._listeners.scroll ? EventListener._listeners.scroll.length : 0;
      this.eventType = "scroll.scrollEvent".concat(date).concat(length);
      this.addScrollEvent();
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.isUnmounted = true;
      EventListener.removeEventListener(this.eventType, this.scrollEventListener, this.target);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
        component = _this$props.component,
        playScale = _this$props.playScale,
        location = _this$props.location,
        targetId = _this$props.targetId,
        onScroll = _this$props.onScroll,
        onChange = _this$props.onChange,
        replay = _this$props.replay,
        componentProps = _this$props.componentProps,
        props = _objectWithoutProperties(_this$props, _excluded);
      return /*#__PURE__*/React.createElement(component, _objectSpread({}, props, componentProps));
    }
  }], [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(props, _ref) {
      var prevProps = _ref.prevProps,
        $self = _ref.$self;
      var nextState = {
        prevProps: props
      };
      if (prevProps && props !== prevProps) {
        $self.scrollEventListener();
      }
      return nextState; // eslint-disable-line
    }
  }]);
}(React.Component);
_defineProperty(ScrollElement, "propTypes", {
  component: PropTypes.any,
  playScale: PropTypes.any,
  id: PropTypes.string,
  onChange: PropTypes.func,
  onScroll: PropTypes.func,
  location: PropTypes.string,
  targetId: PropTypes.string,
  replay: PropTypes.bool,
  componentProps: PropTypes.object
});
_defineProperty(ScrollElement, "defaultProps", {
  component: 'div',
  onChange: noop,
  onScroll: noop,
  playScale: 0.5,
  replay: false,
  componentProps: {}
});
ScrollElement.isScrollElement = true;
export default ScrollElement;